﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SMP.Windows.Utility
{    
    public class Validator
    {
        /// <summary>
        /// Kiểm tra giá trị rỗng khi nhập liệu trên grid.
        /// </summary>
        /// <param name="controlsAndMessage">Hashtable obj</param>
        /// <param name="gridView">gridView obj</param>
        /// <returns></returns>
        /// 
        public static bool CheckNullGrid(System.Collections.Hashtable controlsAndMessage, DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            bool isValid = true;
            for (int i = 0; i < gridView.RowCount; i++)
            {
                gridView.SelectRow(i);
                //if (gridView.GetChildRowCount(i) > 0)  continue;
                if (gridView.IsDataRow(i))
                    foreach (object key in controlsAndMessage.Keys)
                    {
                        //if GridColumn
                        if (key.GetType() == typeof(DevExpress.XtraGrid.Columns.GridColumn)
                        || key.GetType().BaseType == typeof(DevExpress.XtraGrid.Columns.GridColumn))
                        {
                            DevExpress.XtraGrid.Columns.GridColumn GridColumn = (DevExpress.XtraGrid.Columns.GridColumn)key;
                            gridView.FocusedRowHandle = i;
                            gridView.FocusedColumn = GridColumn;
                            if ((gridView.GetRowCellValue(i, GridColumn) + "").Trim() == "")
                            {
                                isValid = false;
                                string values = GridColumn.Caption;
                                MessageBox.Show(values.Substring(0,1).ToUpper() + values.Substring(1).ToLower() + " không được rỗng. \n Vui lòng nhập lại "+
                                                                      values.Substring(0,1).ToLower() + values.Substring(1).ToLower());
                                break;
                            }
                        }

                    }
                if (!isValid)
                    break;
            }
            return isValid;
        }

        /// <summary>
        /// Kiểm tra rỗng trên các control.
        /// </summary>
        /// <param name="controlsAndMessage">Hashtable obj</param>
        /// <returns></returns>
        public static bool CheckNullFields(System.Collections.Hashtable controlsAndMessage)
        {
            bool isValid = true;

            foreach (object key in controlsAndMessage.Keys)
            {
                System.Windows.Forms.Control control = (System.Windows.Forms.Control)key;
                if (control.Text.Trim() == "")
                {                    
                    isValid = false;
                    var values = controlsAndMessage[key];
                    System.Windows.Forms.Control lbtext = (System.Windows.Forms.Control)values;
                    Windows.Forms.Message.Show(lbtext.Text + " không được rỗng. \n Vui lòng nhập lại " + lbtext.Text, "Thông Báo", MessageBoxButtons.OK);
                    control.Focus();
                    break;
                }

            }

            return isValid;
        }

       

       
       


    }
}
